/*     */ package com.zimbra.cs.util;
/*     */ 
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.DateUtil;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.LogFactory;
/*     */ import com.zimbra.cs.account.Entry;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Provisioning.MailMode;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import java.io.PrintStream;
/*     */ import java.util.Formatter;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ class ProxyConfVar
/*     */ {
/*     */   public String mKeyword;
/*     */   public String mAttribute;
/*     */   public ProxyConfValueType mValueType;
/*     */   public Object mDefault;
/*     */   public Object mValue;
/*     */   public ProxyConfOverride mOverride;
/*     */   public String mDescription;
/* 106 */   protected static Log mLog = LogFactory.getLog(ProxyConfGen.class);
/* 107 */   protected static Provisioning mProv = Provisioning.getInstance();
/* 108 */   public static Entry configSource = null;
/* 109 */   public static Entry serverSource = null;
/*     */   
/*     */ 
/*     */   public ProxyConfVar(String keyword, String attribute, Object defaultValue, ProxyConfValueType valueType, ProxyConfOverride overrideType, String description)
/*     */   {
/* 114 */     this.mKeyword = keyword;
/* 115 */     this.mAttribute = attribute;
/* 116 */     this.mValueType = valueType;
/* 117 */     this.mDefault = defaultValue;
/* 118 */     this.mOverride = overrideType;
/* 119 */     this.mValue = this.mDefault;
/* 120 */     this.mDescription = description;
/*     */   }
/*     */   
/*     */   public String confValue() throws ProxyConfException
/*     */   {
/* 125 */     return format(this.mValue);
/*     */   }
/*     */   
/*     */   public Object rawValue()
/*     */   {
/* 130 */     return this.mValue;
/*     */   }
/*     */   
/*     */   public void write(PrintStream ps) throws ProxyConfException
/*     */   {
/* 135 */     ps.println("  NGINX Keyword:         " + this.mKeyword);
/* 136 */     ps.println("  Description:           " + this.mDescription);
/* 137 */     ps.println("  Value Type:            " + this.mValueType.toString());
/* 138 */     ps.println("  Controlling Attribute: " + (this.mAttribute == null ? "(none)" : this.mAttribute));
/* 139 */     ps.println("  Default Value:         " + (this.mDefault == null ? "(none)" : this.mDefault.toString()));
/* 140 */     ps.println("  Current Value:         " + (this.mValue == null ? "(none)" : this.mValue.toString()));
/* 141 */     ps.println("  Config Text:           " + (this.mValue == null ? "(none)" : format(this.mValue)));
/* 142 */     ps.println("");
/*     */   }
/*     */   
/*     */   public void update()
/*     */     throws ServiceException, ProxyConfException
/*     */   {
/* 148 */     if (this.mOverride == ProxyConfOverride.NONE) {
/* 149 */       return;
/*     */     }
/*     */     
/* 152 */     if (this.mValueType == ProxyConfValueType.INTEGER) {
/* 153 */       updateInteger();
/* 154 */     } else if (this.mValueType == ProxyConfValueType.LONG) {
/* 155 */       updateLong();
/* 156 */     } else if (this.mValueType == ProxyConfValueType.STRING) {
/* 157 */       updateString();
/* 158 */     } else if (this.mValueType == ProxyConfValueType.BOOLEAN) {
/* 159 */       updateBoolean();
/* 160 */     } else if (this.mValueType == ProxyConfValueType.ENABLER) {
/* 161 */       updateEnabler();
/* 162 */     } else if (this.mValueType == ProxyConfValueType.TIME) {
/* 163 */       updateTime();
/* 164 */     } else if (this.mValueType == ProxyConfValueType.CUSTOM)
/*     */     {
/*     */ 
/* 167 */       throw new ProxyConfException("the custom update of ProxyConfVar with key " + this.mKeyword + " has to be implementated by override");
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   public String format(Object o)
/*     */     throws ProxyConfException
/*     */   {
/* 175 */     if (this.mValueType == ProxyConfValueType.INTEGER)
/* 176 */       return formatInteger(o);
/* 177 */     if (this.mValueType == ProxyConfValueType.LONG)
/* 178 */       return formatLong(o);
/* 179 */     if (this.mValueType == ProxyConfValueType.STRING)
/* 180 */       return formatString(o);
/* 181 */     if (this.mValueType == ProxyConfValueType.BOOLEAN)
/* 182 */       return formatBoolean(o);
/* 183 */     if (this.mValueType == ProxyConfValueType.ENABLER)
/* 184 */       return formatEnabler(o);
/* 185 */     if (this.mValueType == ProxyConfValueType.TIME) {
/* 186 */       return formatTime(o);
/*     */     }
/* 188 */     throw new ProxyConfException("the custom format of ProxyConfVar with key " + this.mKeyword + " has to be implementated by override");
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public void updateString()
/*     */   {
/* 195 */     if (this.mOverride == ProxyConfOverride.CONFIG) {
/* 196 */       this.mValue = configSource.getAttr(this.mAttribute, (String)this.mDefault);
/* 197 */     } else if (this.mOverride == ProxyConfOverride.LOCALCONFIG) {
/* 198 */       this.mValue = lcValue(this.mAttribute, (String)this.mDefault);
/* 199 */     } else if (this.mOverride == ProxyConfOverride.SERVER) {
/* 200 */       this.mValue = serverSource.getAttr(this.mAttribute, (String)this.mDefault);
/*     */     }
/*     */   }
/*     */   
/*     */   public String formatString(Object o) {
/* 205 */     Formatter f = new Formatter();
/* 206 */     f.format("%s", new Object[] { o });
/* 207 */     return f.toString();
/*     */   }
/*     */   
/*     */   public void updateBoolean() {
/* 211 */     if (this.mOverride == ProxyConfOverride.CONFIG) {
/* 212 */       this.mValue = Boolean.valueOf(configSource.getBooleanAttr(this.mAttribute, ((Boolean)this.mDefault).booleanValue()));
/*     */     }
/* 214 */     else if (this.mOverride == ProxyConfOverride.LOCALCONFIG) {
/* 215 */       this.mValue = Boolean.valueOf(lcValue(this.mAttribute, this.mDefault.toString()));
/* 216 */     } else if (this.mOverride == ProxyConfOverride.SERVER) {
/* 217 */       this.mValue = Boolean.valueOf(serverSource.getBooleanAttr(this.mAttribute, ((Boolean)this.mDefault).booleanValue()));
/*     */     }
/*     */   }
/*     */   
/*     */   public String formatBoolean(Object o)
/*     */   {
/* 223 */     if (((Boolean)o).booleanValue())
/* 224 */       return "on";
/* 225 */     return "off";
/*     */   }
/*     */   
/*     */   public void updateEnabler() {
/* 229 */     updateBoolean();
/*     */   }
/*     */   
/*     */   public String formatEnabler(Object o) {
/* 233 */     if (((Boolean)o).booleanValue())
/* 234 */       return "";
/* 235 */     return "#";
/*     */   }
/*     */   
/*     */   public void updateTime() {
/* 239 */     if (this.mOverride == ProxyConfOverride.CONFIG) {
/* 240 */       this.mValue = new Long(configSource.getTimeInterval(this.mAttribute, ((Long)this.mDefault).longValue()));
/*     */     }
/* 242 */     else if (this.mOverride == ProxyConfOverride.LOCALCONFIG) {
/* 243 */       this.mValue = new Long(DateUtil.getTimeInterval(lcValue(this.mAttribute, this.mDefault.toString()), ((Long)this.mDefault).longValue()));
/*     */ 
/*     */     }
/* 246 */     else if (this.mOverride == ProxyConfOverride.SERVER) {
/* 247 */       this.mValue = new Long(serverSource.getTimeInterval(this.mAttribute, ((Long)this.mDefault).longValue()));
/*     */     }
/*     */   }
/*     */   
/*     */   public String formatTime(Object o)
/*     */   {
/* 253 */     Formatter f = new Formatter();
/* 254 */     f.format("%dms", new Object[] { (Long)o });
/* 255 */     return f.toString();
/*     */   }
/*     */   
/*     */   public void updateInteger() {
/* 259 */     if (this.mOverride == ProxyConfOverride.CONFIG) {
/* 260 */       this.mValue = new Integer(configSource.getIntAttr(this.mAttribute, ((Integer)this.mDefault).intValue()));
/*     */     }
/* 262 */     else if (this.mOverride == ProxyConfOverride.LOCALCONFIG) {
/* 263 */       this.mValue = Integer.valueOf(lcValue(this.mAttribute, this.mDefault.toString()));
/* 264 */     } else if (this.mOverride == ProxyConfOverride.SERVER) {
/* 265 */       this.mValue = new Integer(serverSource.getIntAttr(this.mAttribute, ((Integer)this.mDefault).intValue()));
/*     */     }
/*     */   }
/*     */   
/*     */   public String formatInteger(Object o)
/*     */   {
/* 271 */     Formatter f = new Formatter();
/* 272 */     f.format("%d", new Object[] { (Integer)o });
/* 273 */     return f.toString();
/*     */   }
/*     */   
/*     */   public void updateLong() {
/* 277 */     if (this.mOverride == ProxyConfOverride.CONFIG) {
/* 278 */       this.mValue = new Long(configSource.getLongAttr(this.mAttribute, ((Long)this.mDefault).longValue()));
/*     */     }
/* 280 */     else if (this.mOverride == ProxyConfOverride.LOCALCONFIG) {
/* 281 */       this.mValue = Long.valueOf(lcValue(this.mAttribute, this.mDefault.toString()));
/* 282 */     } else if (this.mOverride == ProxyConfOverride.SERVER) {
/* 283 */       this.mValue = new Long(serverSource.getLongAttr(this.mAttribute, ((Long)this.mDefault).longValue()));
/*     */     }
/*     */   }
/*     */   
/*     */   public String formatLong(Object o)
/*     */   {
/* 289 */     Formatter f = new Formatter();
/* 290 */     Long l = (Long)o;
/*     */     
/* 292 */     if (l.longValue() % 1048576L == 0L) {
/* 293 */       f.format("%dm", new Object[] { Long.valueOf(l.longValue() / 1048576L) });
/* 294 */     } else if (l.longValue() % 1024L == 0L) {
/* 295 */       f.format("%dk", new Object[] { Long.valueOf(l.longValue() / 1024L) });
/*     */     } else
/* 297 */       f.format("%d", new Object[] { l });
/* 298 */     return f.toString();
/*     */   }
/*     */   
/*     */   private String lcValue(String key, String def) {
/* 302 */     String val = LC.get(key);
/*     */     
/* 304 */     return (val == null) || (val.length() == 0) ? def : val;
/*     */   }
/*     */   
/*     */   boolean isValidUpstream(Server server, String serverName) {
/* 308 */     boolean isTarget = server.getBooleanAttr("zimbraReverseProxyLookupTarget", false);
/*     */     
/* 310 */     if (!isTarget) {
/* 311 */       return false;
/*     */     }
/*     */     
/* 314 */     String mode = server.getAttr("zimbraMailMode", "");
/* 315 */     if ((mode.equalsIgnoreCase(Provisioning.MailMode.http.toString())) || (mode.equalsIgnoreCase(Provisioning.MailMode.mixed.toString())) || (mode.equalsIgnoreCase(Provisioning.MailMode.both.toString())) || (mode.equalsIgnoreCase(Provisioning.MailMode.redirect.toString())) || (mode.equalsIgnoreCase(Provisioning.MailMode.https.toString())))
/*     */     {
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 324 */       return true;
/*     */     }
/* 326 */     mLog.warn("Upstream: Ignoring server: " + serverName + " ,because its mail mode is: " + (mode.equals("") ? "EMPTY" : mode));
/*     */     
/* 328 */     return false;
/*     */   }
/*     */   
/*     */   String generateServerDirective(Server server, String serverName, String portName)
/*     */   {
/* 333 */     int serverPort = server.getIntAttr(portName, 0);
/* 334 */     int timeout = server.getIntAttr("zimbraMailProxyReconnectTimeout", 60);
/*     */     
/* 336 */     String version = server.getAttr("zimbraServerVersion", "");
/* 337 */     int maxFails = server.getIntAttr("zimbraMailProxyMaxFails", 1);
/* 338 */     if ((maxFails != 1) && (version != "")) {
/* 339 */       return String.format("%s:%d fail_timeout=%ds max_fails=%d version=%s", new Object[] { serverName, Integer.valueOf(serverPort), Integer.valueOf(timeout), Integer.valueOf(maxFails), version });
/*     */     }
/* 341 */     if (maxFails != 1) {
/* 342 */       return String.format("%s:%d fail_timeout=%ds max_fails=%d", new Object[] { serverName, Integer.valueOf(serverPort), Integer.valueOf(timeout), Integer.valueOf(maxFails) });
/*     */     }
/* 344 */     if (version != "") {
/* 345 */       return String.format("%s:%d fail_timeout=%ds version=%s", new Object[] { serverName, Integer.valueOf(serverPort), Integer.valueOf(timeout), version });
/*     */     }
/*     */     
/* 348 */     return String.format("%s:%d fail_timeout=%ds", new Object[] { serverName, Integer.valueOf(serverPort), Integer.valueOf(timeout) });
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/util/ProxyConfVar.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */